import { login, logout, getInfo } from '@/api/acl/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import {constantRoutes,asyncRoutes,anyRoutes } from '@/router/routes'
import router from '@/router'
import cloneDeep from 'lodash/cloneDeep'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',

    buttons:[],
    routes:[],
    roles:'',
    asyncRoutes:[]
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERS:(state,{buttons,name,roles,avatar})=>{
    state.buttons=buttons
    state.name=name
    state.roles= roles
    state.avatar = avatar
  },
  SET_ROUTES:(state,asyncRoutes)=>{
    state.asyncRoutes = asyncRoutes
    state.routes = constantRoutes.concat(asyncRoutes,anyRoutes)
    router.addRoutes([...asyncRoutes,anyRoutes])
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        console.log(data.token,66666666);
        commit('SET_TOKEN', data.token.accessToken)
        setToken(data.token.accessToken)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        commit('SET_USERS',data)
       commit('SET_ROUTES',filterAscyncRoutes(cloneDeep(asyncRoutes),data.routes))
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

function filterAscyncRoutes(routes,routeName){
    return routes.filter(route =>{
      if(routeName.includes(route.name)){
        if(route.children&&route.children.length){
            const cRoute = filterAscyncRoutes(route.children,routeName)
            if(cRoute && cRoute.length){
              route.children = cRoute
              return true
            }
            return false
        }
        return true
      }
      return false
    }) 
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

